package views;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.*;

import controllers.Driver;
import controllers.TimeSlot;
import db.DriverDAO;
import db.TimeSlotDAO;

public class AddDriverFrame extends JFrame {
	
	private static final int WIDTH=500;
	private static final int HEIGHT=300;
	private static final String TITLE = "Add Driver";
	private JLabel nameLabel, phoneNumberLabel, emailLabel, timeSlotLabel;
	private JTextField name, phoneNumber, email;
	private JButton save, addTimeSlot, removeTimeSlot;
	private static JList timeSlots;
	private static DefaultListModel model;
	
	public AddDriverFrame(int xBound, int yBound){
		this.setTitle(TITLE);
		this.setBounds(xBound+100, yBound+100, WIDTH, HEIGHT);
		this.setResizable(false);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		Container c = this.getContentPane();
		
		KeyListener l = new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
				enableSave();
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableSave();	
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
				enableSave();	
			}
		};
		
		nameLabel = new JLabel("Name");
		nameLabel.setBounds(25, 25, 200, 25);
		panel.add(nameLabel);
		
		name = new JTextField();
		name.setBounds(25, 50, 200, 25);
		name.addKeyListener(l);
		panel.add(name);
		
		emailLabel = new JLabel("Email Address");
		emailLabel.setBounds(25, 75, 200, 25);
		panel.add(emailLabel);
		
		email = new JTextField();
		email.setBounds(25, 100, 200, 25);
		email.addKeyListener(l);
		panel.add(email);
		
		phoneNumberLabel = new JLabel("Phone Number");
		phoneNumberLabel.setBounds(25, 125, 200, 25);
		panel.add(phoneNumberLabel);
		
		phoneNumber = new JTextField();
		phoneNumber.setBounds(25, 150, 200, 25);
		phoneNumber.addKeyListener(l);
		panel.add(phoneNumber);
		
		timeSlotLabel = new JLabel("Time Slots");
		timeSlotLabel.setBounds(250, 25, 150, 25);
		panel.add(timeSlotLabel);
		
		model = new DefaultListModel();
		timeSlots = new JList(model);
		timeSlots.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		timeSlots.setLayoutOrientation(JList.VERTICAL);
		JScrollPane pane = new JScrollPane(timeSlots);
		pane.setBounds(250, 50, 200, 125);
		panel.add(pane);
		

		addTimeSlot = new JButton("Add");
		addTimeSlot.setBounds(250, 180, 100, 30);
		addTimeSlot.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new AddDriverTimeSlotFrame(getX(),getY());
			}
		});
		panel.add(addTimeSlot);
		
		removeTimeSlot = new JButton("Remove");
		removeTimeSlot.setBounds(350, 180, 100, 30);
		removeTimeSlot.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				model.remove(timeSlots.getSelectedIndex());
			}
		});
		panel.add(removeTimeSlot);
		
		
		save = new JButton("Save");
		save.setBounds(25, 200, 100, 30);
		save.setEnabled(false);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Driver d = new Driver();
				d.setName(name.getText());
				d.setEmail(email.getText());
				d.setPhoneNumber(phoneNumber.getText());
				DriverDAO.addDriver(d);
				List<Driver> drivers = DriverDAO.getDrivers();
				int driverId = drivers.get(drivers.size()-1).getId();
				for (int i=0;i<model.getSize();i++)
				{
					TimeSlotDAO.addTimeSlot((TimeSlot)model.get(i),driverId);
				}
				ViewDriversFrame.updateRows();
				AddDriverFrame.this.dispose();
			}
		});
		panel.add(save);
		
		c.add(panel);
		
		this.setVisible(true);
		
	}
	
	public void enableSave(){
		if(name.getText().length()>0&&email.getText().length()>0&&phoneNumber.getText().length()>0){
			save.setEnabled(true);
		}
		else{
			save.setEnabled(false);
		}
	}
	
	public static void addTimeSlot(TimeSlot ts){
		model.addElement(ts);
	}

}
